//index.js
//获取应用实例
const app = getApp()
const userUrl = require('../../config.js').userUrl
const courseId = require('../../config.js').courseId

Page({
  data: {
    current_course: {},
    // motto: 'Hello World',
    // userInfo: {},
    // hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo')
    ques_count:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onShow:function(){
    var that = this
    wx.request({
      url: userUrl+'getDoneQuesCount',
      data:{
        openid:wx.getStorageSync('jiaoxue_OPENID'),
        courseId:courseId
      },
      success:function(res){
        // console.log(res)
        that.setData({
          ques_count:res.data.msg
        })
      }
    })
  },

  exercise(e) {
    console.log(e)
    var _url
    let type = e.currentTarget.dataset.type
    if (type == 'sxlx') {
      _url = "/pages/answer/answer_info/info?subject=&type=sxlx"
    } else if (type == 'zjlx') {
      _url = "/pages/answer/answer_chapter/chapter?subject=&type=zjlx"
    } else if (type == 'ztlx') {
      _url = "/pages/answer/answer_classify/classify?subject=&type=ztlx"
    }
    wx.navigateTo({
      url: _url,
    })
  },

  bindUrlToStore: function (f) {
    var collection = f.currentTarget.dataset.collection,
      subject = f.currentTarget.dataset.urlparem
    if (!!collection) {
      wx.navigateTo({
        url: '/pages/answer/answer_info/info?subject=subject&type=wdsc',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '未发现你的收藏',
        showCancel: false,
        confirmText: '知道了',
        success: function (res) {

        }
      })
    }
  },

  bindUrlToWrong: function (f) {
    var answerError = f.currentTarget.dataset.answererror,
      subject = f.currentTarget.dataset.urlparem;
    if (!!answerError) {
      wx.navigateTo({
        url: '/pages/answer/answer_info/info?subject=subject&type=wdct',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '恭喜你,暂无错题',
        showCancel: false,
        confirmText: '知道了',
        confirmColor:'#00bcd5',
        success: function (res) {

        }
      })
    }
  },

  onLoad: function() {
    this.getCurrentCourse(courseId)
    // if (app.globalData.userInfo) {
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true
    //   })
    // } else if (this.data.canIUse){
    //   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userInfoReadyCallback = res => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // } else {
    //   // 在没有 open-type=getUserInfo 版本的兼容处理
    //   wx.getUserInfo({
    //     success: res => {
    //       app.globalData.userInfo = res.userInfo
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //     }
    //   })
    // }
  },
  getCurrentCourse(course_id = '') {
    wx.request({
      url: userUrl + 'current',
      data: {
        current_course_id: course_id,
        openid: wx.getStorageSync('jiaoxue_OPENID')
      },
      success: res => {
        console.log('res1', res)
        this.setData({
          current_course: res.data.data
        })
      }
    })
  }
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
})